package com.xgxx.chendawei.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class JsonUtil {
    private static ObjectMapper objectMapper = new ObjectMapper();

    public JsonUtil() {
    }

    public static ObjectMapper getObjectMapper() {
        return objectMapper;
    }

    public static boolean isJSON(String json) {
        try {
            getObjectMapper().readTree(json);
            return true;
        } catch (IOException var2) {
            return false;
        }
    }

    public static String beanToJson(Object o) throws JsonProcessingException {
        String json = getObjectMapper().writeValueAsString(o);
        return json;
    }

    public static <T> T jsonToBean(String json, Class<T> o) throws IOException {
        T result = getObjectMapper().readValue(json, o);
        return result;
    }

    public static <T> T convertValue(Object o, TypeReference<T> toValueTypeRef) {
        return getObjectMapper().convertValue(o, toValueTypeRef);
    }

    public static <T> T convertValue(Object o, Class<T> toValueType) {
        return getObjectMapper().convertValue(o, toValueType);
    }

    public static <T> T convertValue(Object o, JavaType toValueType) {
        return getObjectMapper().convertValue(o, toValueType);
    }
}
